package proTrack.model;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;


@Entity
@NamedQueries({
    @NamedQuery(name="AllJobs",
            query="SELECT j FROM Job j"),
    @NamedQuery(name="JobsByHandler",
            query="SELECT j FROM Job j WHERE j.handler = ?1"),
    @NamedQuery(name="JobsByState",
            query="SELECT j FROM Job j WHERE j.state.name = ?1"),
    @NamedQuery(name="JobsUnassigned",
            query="SELECT j FROM Job j WHERE j.handler = null"),
    @NamedQuery(name="JobsByCreator",
            query="SELECT j FROM Job j WHERE j.creator = ?1"),
    @NamedQuery(name="DeleteJob",
            query="DELETE FROM Job j WHERE j.id = ?1")
                            
    })
public class Job {

	@Id
	@GeneratedValue
	private int id;

	private String text;

	@Column(nullable = false)
	private State state;

	@OneToOne(cascade = {CascadeType.MERGE, CascadeType.REFRESH})
    private Account handler;

	@OneToOne(cascade = {CascadeType.MERGE, CascadeType.REFRESH}, optional = false)
	private Account creator;

	protected Job() {

	}

	public Job(final String text, final Account handler,
			final Account creator, final State state) {
		this.text = text;
		this.handler = handler;
		this.creator = creator;
		
	}


	public int getId() {
		return id;
	}

	protected void setId(final int id) {
		this.id = id;
	}

	public String getText() {
		return text;
	}

	public void setText(final String text) {
		this.text = text;
	}


	public Account getHandler() {
        return handler;
    }

    public void setHandler(final Account user) {
        this.handler = user;
    }

    @Override
	public String toString() {
		return getId() + " " + getText() + " " + getState() + " ";
	}

	public State getState() {
		return state;
	}

	public void setState(final State state) {
		this.state = state;
	}

	public Account getCreator() {
		return creator;
	}

	protected void setCreator(final Account creator) {
		this.creator = creator;
	}
	
	

}
